package cfca.paperless.client.servlet._1_basic._1_03_synthesizeAutoMultiPdfWithPdfId;

import java.io.File;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StringUtil;

/**
 * @Description PDF自动化合成多媒体数据接口，使用示例。
 * 1，操作对象为本地待处理的单个PDF文件。
 * 2，合成内容包含：图片、录音等。 
 * 3，返回成功合成内容的PDF文件。
 * 
 * @Author dell
 * @Date 2017-12-12
 * @CodeReviewer TODO
 */
public class synthesizeAutoMultiPdfWithPdfIdTest01 {

    public static void main(String[] args) throws Exception {

        long timeStart = System.currentTimeMillis();// 开始时间

        System.out.println("synthesizeAutoMultiPdfWithPdfIdTest01 START");

        PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.url, 10000, 60000);

        String pdfFilePath = "./TestData/autoCalculatePdfHash.pdf";
        String pdfSuffix = IoUtil.getFileNameSuffix(pdfFilePath);
        if(!PaperlessConfig.PDF_FILE_SUFFIX.equals(pdfSuffix)){
            throw new Exception("所选文件不是pdf文件");
        } 
        
        // 待合成多媒体数据的PDF文件
        byte[] pdfFileData = FileUtils.readFileToByteArray(new File(pdfFilePath));

        // 保存到临时PDF表中的pdfId，符合唯一约束（可以为空）
        String savedPdfId = GUID.generateId();

        // 需要合成到pdf的多媒体数据，多媒体数据包括图片、录音等
        byte[] handwritingImage = FileUtils.readFileToByteArray(new File("./TestData/handwriting.png"));// 手写签名图片
        byte[] handwritingData = handwritingImage;// 假定为手写签名笔记数据
        byte[] identificationFront = FileUtils.readFileToByteArray(new File("./TestData/1.png"));// 身份证正面照片
        byte[] identificationBack = FileUtils.readFileToByteArray(new File("./TestData/2.png"));// 身份证背面照片

        // 多媒体信息数据，多媒体信息名称，多媒体信息描述，type取值：0=不放到pdf文档中（需要客户自己保存）；1=注释方式；2=附件方式
        MultiDataBean handwritingImageBean = new MultiDataBean(handwritingImage, "handwritingImage.png", "手写签名图片", "2");
        MultiDataBean handwritingDataBean = new MultiDataBean(handwritingData, "handwritingData.dat", "手写签名笔记数据", "2");
        MultiDataBean identificationFrontBean = new MultiDataBean(identificationFront, "identificationFrontImage.png", "身份证正面照片", "2");
        MultiDataBean identificationBackBean = new MultiDataBean(identificationBack, "identificationBackImage.png", "身份证背面照片", "2");
        
        ArrayList<MultiDataBean> multiDataList = new ArrayList<MultiDataBean>();
        multiDataList.add(handwritingImageBean);
        multiDataList.add(handwritingDataBean);
        multiDataList.add(identificationFrontBean);
        multiDataList.add(identificationBackBean);

        String[] proofHashAndMultiData = PaperlessUtil.getProofHashAndMultiData(multiDataList);
        
        String proofHashXmlString = proofHashAndMultiData[0];
        String multiDataString = "[" + proofHashAndMultiData[1] + "]";
        
        System.out.println(proofHashXmlString);
        System.out.println(multiDataString);

        String systemNo =  GUID.generateId();//系统流水号
        
        String result = clientBean.synthesizeAutoMultiPdfWithPdfId(systemNo,pdfFileData, savedPdfId, multiDataString, PaperlessConfig.operatorCode,
                PaperlessConfig.channelCode);

        String code = StringUtil.getNodeText(result, "Code");
        // 返回值Code等于200代表后台处理成功
        if (code.equals("200")) {
            System.out.println("synthesizeAutoMultiPdfWithPdfIdTest01 END OK");
            savedPdfId = StringUtil.getNodeText(result, "SavedPdfId");
            System.out.println("savedPdfId:" + savedPdfId);
        }else {
            // 处理结果为异常，打印出错误码和错误信息
            System.out.println("synthesizeAutoMultiPdfWithPdfIdTest01 END NG");
            System.out.println(result);
        }
 
        long timeEnd = System.currentTimeMillis();// 结束时间
        System.out.println("########## synthesizeAutoMultiPdfWithPdfIdTest01 time used:" + (timeEnd - timeStart) + "##########");
    }

}
